package org.elsfs.cloud.common.mybatis.ext.sql.support.select;


import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.elsfs.cloud.common.util.lang.NamingCase;

/**
 * 子查询选择项实现
 *

 */
@Getter
@RequiredArgsConstructor
public class SubQuerySelectItem implements SelectItem {

    /**
     * 子查询SQL
     */
    private final String subQuerySql;

    /**
     * 字段名别
     */
    private final String fieldAlias;

    /**
     * 构建SQL片段
     *
     * @return SQL片段
     */
    @Override
    public String buildSqlFragment() {
        return String.format(
            "(%S) AS %S",
            this.subQuerySql,
            NamingCase.toUnderlineCase(this.fieldAlias)
        );
    }
}
